var progenitor = "";
var mymta = "";
//var parentsJson = new Object();
var defaultMsg = "Updated successfully";

function getKanNatArray() {
    return [
        ['Kan', 'Kan'],
        ['Nat', 'Nat']
    ];
}
function getPloidyArray() {
    return [
        ['Haploid', 'Haploid'],
        ['Diploid', 'Diploid'],
        ['Undetermined', 'Undetermined']
    ];
}

function getDisruptionModeArray() {
    return [
        ['Inactive Allele', 'Inactive Allele'],
        ['HYG', 'HYG'],
        ['DSD', 'DSD']
    ];
}

function populateGeography(myJson, nvStrain){
    myJson["yoContinent"] = nvStrain.yoContinent;
    myJson["yoCountry"] = nvStrain.yoCountry;
    myJson["yoLocale"] = nvStrain.yoLocale;
    myJson["yoGeoNotes"] = nvStrain.yoGeoNotes;
    myJson["yoEnvSource"] = nvStrain.yoEnvSource;
    myJson["yoEnvNotes"] = nvStrain.yoEnvNotes;
}
function setupInitialIsolate(nvStrain) {
    Ext.QuickTips.init();
    progenitor = nvStrain.name;

    //var yesNoArray = getYesNoUnArray();
    var ploidyArray = [
        ['Haploid', 'Haploid'],
        ['Diploid', 'Diploid'],
        ['Undetermined', 'Undetermined']
    ];
    var disruptionModeArray = [
        ['Inactive Allele', 'Inactive Allele'],
        ['HYG', 'HYG'],
        ['DSD', 'DSD']
    ];


    var yesNoStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getYesNoUnArray()
    });

    var disruptionModeStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : disruptionModeArray
    });

    var chromColorCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'chromeColor',
        id: 'chromeColorId',
        fieldLabel: 'S.c. Chromagar color consistency',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Chromagar color consistent with probable Saccharomyces'
                });
            }
        }
    });
    var gfpTaggedCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'gfpTagged',
        id: 'gfpTaggedId',
        fieldLabel: 'GFP Tagged?',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Yes",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Is HO- strain tagged with GFP?'
                });
            }
        }
    });
    var correctBandCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'correctBand',
        id: 'correctBandId',
        fieldLabel: 'PCR/ITS region, Correct Band Size',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'PCR of ITS region, Correct Band Size'
                });
            }
        }
    });
    var restrictionPatternCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'restrictionPattern',
        id: 'restrictionPatternId',
        fieldLabel: 'Restriction pattern correct for S.c.?',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Restriction digestion pattern of ITS DNA correct for S.c.?'
                });
            }
        }
    });
    var disruptionModeCombo = new Ext.form.ComboBox({
        store: disruptionModeStore,
        name: 'disruptionMode',
        id: 'disruptionModeComboId',
        fieldLabel: 'Disruption mode',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"HYG",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Mode of disruption?'
                });
            }
        }
    });

    var disruptionModeCombo2 = new Ext.form.ComboBox({
        store: disruptionModeStore,
        name: 'disruptionMode2',
        id: 'disruptionModeCombo2Id',
        fieldLabel: 'Disruption mode',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"HYG",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Mode of disruption?'
                });
            }
        }
    });


    var diploidSporeCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'diploidSpore',
        id: 'diploidSporeId',
        fieldLabel: 'Sporulation',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Indicates whether Diploid formed spores'
                });
            }
        }
    });

    var sporeViabilityCombo = new Ext.form.ComboBox({
        store: yesNoStore,
        name: 'sporeViability',
        id: 'sporeViabilityId',
        fieldLabel: 'Viability',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Indicates whether spores are viable'
                });
            }
        }
    });

    var ploidyStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : ploidyArray
    });
    var ploidyCombo = new Ext.form.ComboBox({
        store: ploidyStore,
        name: 'ploidy',
        id: 'ploidyId',
        fieldLabel: 'Ploidy',
        displayField:'value',
        valueField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:"Undetermined",
        forceSelection:true,
        selectOnFocus:true,
        listeners: {
            render: function(c) {
                Ext.QuickTips.register({
                    target: c,
                    title: '',
                    text: 'Ploidy - put in ploidy determination evidence in notes'
                });
            }
        }
    });

    var yoICForm = new Ext.FormPanel(
    {
        title   : 'YO Isolation and Construction',
        height: 600,
        width   : 450,
        autoScroll: true,
        bodyStyle: 'padding: 5px',
        items : [
            {
                layout: 'form',
                items: [
                    {
                        xtype: 'fieldset',
                        title: 'Initial Isolate -> Homozygous Diploid',
                        autoWidth: true,
                        collapsible: true,
                        items: [
                            {
                                xtype: 'textfield',
                                fieldLabel: 'Initial Isolate Parent',
                                value: nvStrain.name,
                                name: 'iiParent',
                                id: 'iiParentId',
                                width: 300,
                                flex: 1,
                                allowBlank: false,
                                listeners: {change:
                                        function(field, nv, ov) {
                                            field.setRawValue(transformYOKeyEntry(nv));
                                        }
                                }
                            },
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'Homozygous Diploid Replicates',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        id: 'iiStrain1Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'iiStrain2Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'iiStrain3Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'iiStrain4Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    }
                                ]
                            },
                            chromColorCombo,
                            correctBandCombo,
                            {
                                layout: 'form',
                                width: 425,
                                items: [
                                    restrictionPatternCombo,
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: 'Gel Pix Link',
                                        id: 'gelPixLinkId',
                                        width: 300
                                    }
                                ]
                            },
                            {
                                layout: 'form',
                                width: 425,
                                items: [
                                    ploidyCombo,
                                    {
                                        xtype: 'textarea',
                                        fieldLabel: 'Ploidy Evidence',
                                        id: 'ploidyNotesId',
                                        width: 300,
                                        height: 40
                                    }]
                            },
                            {
                                layout: 'form',
                                width: 425,
                                items: [
                                    diploidSporeCombo,
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: 'Percentage',
                                        id: 'percentSporulationId',
                                        width: 300
                                    }]
                            },
                            {
                                layout: 'form',
                                width: 425,
                                items: [
                                    sporeViabilityCombo,
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: 'Percentage',
                                        id: 'percentViableId',
                                        width: 300
                                    }]
                            },
                            {
                                xtype: 'button',
                                fieldAlign: 'center',
                                text: '&nbsp;&nbsp;Save&nbsp;&nbsp;',
                                id: 'isolationConstructionButtonId',
                                listeners:{click:
                                        function() {
                                            var iiParent = Ext.getCmp("iiParentId").getValue();
                                            var colorConsistency = Ext.getCmp("chromeColorId").getValue();
                                            var correctBandSize = Ext.getCmp("correctBandId").getValue();
                                            var restrictionPatternCorrect = Ext.getCmp("restrictionPatternId").getValue();
                                            var gelPixLink = Ext.getCmp("gelPixLinkId").getValue();
                                            var ploidy = Ext.getCmp("ploidyId").getValue();
                                            var ploidyEvidence = Ext.getCmp("ploidyNotesId").getValue();
                                            var sporulation = Ext.getCmp("diploidSporeId").getValue();
                                            var percentSporulation = Ext.getCmp("percentSporulationId").getValue();
                                            var viability = Ext.getCmp("sporeViabilityId").getValue();
                                            var percentViable = Ext.getCmp("percentViableId").getValue();
                                            var iiJson = {};
                                            iiJson["yoKey"] = iiParent;
                                            iiJson["yoStatus"] = "Initial Isolate";
                                            iiJson["yoCorrectBandSize"] = correctBandSize;
                                            iiJson["yoColorConsistency"] = colorConsistency;
                                            iiJson["yoRestrictionPatternCorrect"] = restrictionPatternCorrect;
                                            iiJson["yoGelPixLink"] = gelPixLink;
                                            iiJson["yoPloidy"] = ploidy;
                                            iiJson["yoPloidyEvidence"] = ploidyEvidence;
                                            iiJson["yoSporulation"] = sporulation;
                                            iiJson["yoPercentSporulation"] = percentSporulation;
                                            iiJson["yoViability"] = viability;
                                            iiJson["yoPercentViable"] = percentViable;
                                            populateGeography(iiJson, nvStrain);
                                            //iiJson["mta"] = mymta;
                                            //var iiParentUri = "/addama-rest/dudley-sample/path/datasource/YO/" + iiParent + "/annotations";
                                            //postDudleyAnnotations(iiParentUri, iiJson, defaultCallback);
                                            postSolrUpdates(iiJson, defaultCallback);
                                            //update spores
                                            var hodRepl1 = Ext.getCmp("iiStrain1Id").getValue();
                                            if (hodRepl1 == null) {
                                                hodRepl1 = "";
                                            }
                                            var hodRepl2 = Ext.getCmp("iiStrain2Id").getValue();
                                            if (hodRepl2 == null) {
                                                hodRepl2 = "";
                                            }
                                            var hodRepl3 = Ext.getCmp("iiStrain3Id").getValue();
                                            if (hodRepl3 == null) {
                                                hodRepl3 = "";
                                            }
                                            var hodRepl4 = Ext.getCmp("iiStrain4Id").getValue();
                                            if (hodRepl4 == null) {
                                                hodRepl4 = "";
                                            }
                                            var iiSiblingArray = [];
                                            iiSiblingArray[0] = hodRepl1;
                                            iiSiblingArray[1] = hodRepl2;
                                            iiSiblingArray[2] = hodRepl3;
                                            iiSiblingArray[3] = hodRepl4;
                                            for (var s = 0; s < iiSiblingArray.length; s++) {
                                                var progenyKey = iiSiblingArray[s];
                                                if (progenyKey != "") {
                                                    postProgenyAnnotations(progenyKey, iiSiblingArray, iiParent, "Homozygous Diploid", nvStrain);
                                                }
                                            }

                                        }}
                            }
                        ]
                    },
                    {
                        xtype: 'fieldset',
                        title: 'Homozygous Diploid -> HO- Hemizygous',
                        width: 450,
                        collapsed: true,
                        collapsible: true,
                        items: [
                            {
                                xtype: 'textfield',
                                fieldLabel: 'Homozygous Diploid Parent',
                                id: 'homozygousParentId',
                                width: 300,
                                flex: 1,
                                allowBlank: false,
                                listeners: {change:
                                        function(field, nv, ov) {
                                            field.setRawValue(transformYOKeyEntry(nv));
                                        }
                                }
                            },
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'HO- Hemizygous Replicates',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        id: 'hohemi1Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'hohemi2Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }

                                    }
                                ]
                            },
                            disruptionModeCombo,
                            {
                                xtype: 'button',
                                fieldAlign: 'center',
                                text: '&nbsp;&nbsp;Save&nbsp;&nbsp;',
                                id: 'homozygousDiploidButtonId',
                                listeners:{click:
                                        function() {
                                            var hohParent = Ext.getCmp("homozygousParentId").getValue();
                                            var hohemiRepl1 = Ext.getCmp("hohemi1Id").getValue();
                                            var hohemiRepl2 = Ext.getCmp("hohemi2Id").getValue();
                                            var disruptionMode = Ext.getCmp("disruptionModeComboId").getValue();
                                            //var hohParentUri = "/addama-rest/dudley-sample/path/datasource/YO/" + hohParent + "/annotations";
                                            var jsonParentPost = {};
                                            jsonParentPost["yoDisruptionMode"] = disruptionMode;
                                            jsonParentPost["yoKey"] = hohParent;

                                            getProgenitor("yoKey:" + hohParent, function() {
                                                postSolrUpdates(jsonParentPost, function() {
                                                    var hohemiJson = {};
                                                    hohemiJson["yoStatus"] = "HO- Hemizygous";
                                                    hohemiJson["yoParent"] = hohParent;
                                                    hohemiJson["yoProgenitor"] = progenitor;
                                                    hohemiJson["yoMTA"] = mymta;

                                                    if (hohemiRepl1 != null && hohemiRepl1.length > 1) {
                                                        //var hohemiUri = "/addama-rest/dudley-sample/path/datasource/YO/" + hohemiRepl1 + "/annotations";
                                                        hohemiJson["yoKey"] = hohemiRepl1;
                                                        if (hohemiRepl2 != null && hohemiRepl2.length > 1) {
                                                            hohemiJson["yoSiblings"] = hohemiRepl2;
                                                        }
                                                        populateGeography(hohemiJson, nvStrain);
                                                        //postDudleyAnnotations(hohemiUri, hohemiJson, defaultCallback);
                                                        postSolrUpdates(hohemiJson, defaultCallback);
                                                    }
                                                    if (hohemiRepl2 != null && hohemiRepl2.length > 1) {
                                                        //var hohemi2Uri = "/addama-rest/dudley-sample/path/datasource/YO/" + hohemiRepl2 + "/annotations";
                                                        hohemiJson["yoKey"] = hohemiRepl2;
                                                        if (hohemiRepl1 != null && hohemiRepl1.length > 1) {
                                                            hohemiJson["yoSiblings"] = hohemiRepl1;
                                                        } else {
                                                            hohemiJson["yoSiblings"] = "";
                                                        }
                                                        //postDudleyAnnotations(hohemi2Uri, hohemiJson, defaultCallback);
                                                        postSolrUpdates(hohemiJson, defaultCallback);
                                                    }
                                                });
                                            });
                                        }
                                }
                            }
                        ]
                    },
                    {
                        xtype: 'fieldset',
                        title: 'HO- Hemizygous -> HO- Haploid',
                        width: 450,
                        collapsed: true,
                        collapsible: true,
                        items: [
                            {
                                xtype: 'textfield',
                                fieldLabel: 'HO- Hemizygous Parent',
                                id: 'hemiParentId',
                                width: 300,
                                flex: 1,
                                allowBlank: false,
                                listeners: {change:
                                        function(field, nv, ov) {
                                            field.setRawValue(transformYOKeyEntry(nv));
                                        }
                                }
                            },
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'HO- Haploid Replicates',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        value: 'a: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'hohRepl1Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'hohRepl2Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }

                                    },
                                    /*{
                                     xtype: 'textfield',
                                     id: 'hohRepl3Id',
                                     width: 50,
                                     flex: 1,
                                     allowBlank: false,
                                     margins: '0 5 0 0',
                                     listeners: {change:
                                     function(field, nv, ov) {
                                     field.setRawValue(transformYOKeyEntry(nv));
                                     }
                                     }

                                     },*/
                                    {
                                        xtype: 'displayfield',
                                        value: 'alpha: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'hohRepl3Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'hohRepl4Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    }
                                ]
                            },
                            disruptionModeCombo2,
                            {
                                xtype: 'button',
                                fieldAlign: 'center',
                                text: '&nbsp;&nbsp;Save&nbsp;&nbsp;',
                                id: 'hemizygousHOButtonId',
                                listeners:{click:
                                        function() {
                                            var hemiParent = Ext.getCmp("hemiParentId").getValue();
                                            var hohRepl1 = Ext.getCmp("hohRepl1Id").getValue();
                                            if (hohRepl1 == null) {
                                                hohRepl1 = "";
                                            }
                                            var hohRepl2 = Ext.getCmp("hohRepl2Id").getValue();
                                            if (hohRepl2 == null) {
                                                hohRepl2 = "";
                                            }
                                            var hohRepl3 = Ext.getCmp("hohRepl3Id").getValue();
                                            if (hohRepl3 == null) {
                                                hohRepl3 = "";
                                            }
                                            var hohRepl4 = Ext.getCmp("hohRepl4Id").getValue();
                                            if (hohRepl4 == null) {
                                                hohRepl4 = "";
                                            }
                                            /*var hohRepl5 = Ext.getCmp("hohRepl5Id").getValue();
                                             if (hohRepl5 == null) {
                                             hohRepl5 = "";
                                             }
                                             var hohRepl6 = Ext.getCmp("hohRepl6Id").getValue();
                                             if (hohRepl6 == null) {
                                             hohRepl6 = "";
                                             }*/

                                            var siblingArray = [];
                                            siblingArray[0] = hohRepl1;
                                            siblingArray[1] = hohRepl2;
                                            siblingArray[2] = hohRepl3;
                                            siblingArray[3] = hohRepl4;

                                            var disruptionMode = Ext.getCmp("disruptionModeCombo2Id").getValue();
                                            //var hohParentUri = "/addama-rest/dudley-sample/path/datasource/YO/" + hemiParent + "/annotations";
                                            var jsonParentPost = {};
                                            jsonParentPost["yoKey"] = hemiParent;                                            
                                            jsonParentPost["yoDisruptionMode"] = disruptionMode;
                                            getProgenitor("yoKey:" + hemiParent, function() {
                                                //postDudleyAnnotations(hohParentUri, jsonParentPost, defaultCallback);
                                                postSolrUpdates(jsonParentPost, defaultCallback);
                                            });
                                            for (var s = 0; s < siblingArray.length; s++) {
                                                var progenyKey = siblingArray[s];
                                                if (progenyKey != "") {
                                                    postProgenyAnnotations(progenyKey, siblingArray, hemiParent, "HO- Haploid", nvStrain);
                                                }
                                            }
                                        }
                                }
                            }
                        ]
                    },
                    {
                        xtype: 'fieldset',
                        title: 'HO- Haploid -> SPS2-GFP Tagged',
                        width: 450,
                        collapsed: true,
                        collapsible: true,
                        items: [
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'SPS2 Parents',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        value: 'a: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spKanParentId',
                                        width: 40,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'displayfield',
                                        value: 'alpha:'
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spNatParentId',
                                        width: 40,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    }
                                ]
                            },
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'SPS2-GFP Tagged Replicates',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        value: 'a/KAN: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spKan1Id',
                                        width: 30,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 2 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spKan2Id',
                                        width: 30,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 2 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }

                                    },
                                    {
                                        xtype: 'displayfield',
                                        value: 'alpha/NAT:'
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spNat1Id',
                                        width: 30,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 2 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'spNat2Id',
                                        width: 30,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 2 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    }
                                ]
                            },
                            {
                                xtype: 'button',
                                fieldAlign: 'center',
                                text: '&nbsp;&nbsp;Save&nbsp;&nbsp;',
                                id: 'hoHaploidButtonId',
                                listeners:{click:
                                        function() {
                                            var spKanParent = Ext.getCmp("spKanParentId").getValue();
                                            var spKanJson = {};
                                            var spKan1 = Ext.getCmp("spKan1Id").getValue();
                                            var spKan2 = Ext.getCmp("spKan2Id").getValue();
                                            spKanJson["yoStatus"] = "SPS2-GFP Tagged KAN";
                                            spKanJson["yoParent"] = spKanParent;
                                            spKanJson["yoMatingType"] = "A";
                                            spKanJson["yoPloidy"] = "Haploid";
                                            spKanJson["yoMTA"] = mymta;
                                            populateGeography(spKanJson, nvStrain);

                                            if (spKan1 != null && spKan1.length > 1) {
                                                //var spKan1Uri = "/addama-rest/dudley-sample/path/datasource/YO/" + spKan1 + "/annotations";
                                                if (spKan2 != null && spKan2.length > 1) {
                                                    spKanJson["yoSiblings"] = spKan2;
                                                }
                                                getProgenitor("yoKey:" + spKanParent, function() {
                                                    spKanJson["yoProgenitor"] = progenitor;
                                                    //postDudleyAnnotations(spKan1Uri, spKanJson, defaultCallback);
                                                    spKanJson["yoKey"] = spKan1;                                                    
                                                    postSolrUpdates(spKanJson, defaultCallback);

                                                });
                                            }
                                            if (spKan2 != null && spKan2.length > 1) {
                                                //var spKan2Uri = "/addama-rest/dudley-sample/path/datasource/YO/" + spKan2 + "/annotations";
                                                if (spKan1 != null && spKan1.length > 1) {
                                                    spKanJson["yoSiblings"] = spKan1;
                                                } else {
                                                    spKanJson["yoSiblings"] = "";
                                                }
                                                getProgenitor("yoKey:" + spKanParent, function() {
                                                    spKanJson["yoProgenitor"] = progenitor;
                                                    spKanJson["yoKey"] = spKan2;                                                    
                                                    postSolrUpdates(spKanJson, defaultCallback);

                                                    //postDudleyAnnotations(spKan2Uri, spKanJson, defaultCallback);
                                                });
                                            }
                                            var spNatParent = Ext.getCmp("spNatParentId").getValue();
                                            var spNatJson = {};
                                            var spNat1 = Ext.getCmp("spNat1Id").getValue();
                                            var spNat2 = Ext.getCmp("spNat2Id").getValue();
                                            spNatJson["yoStatus"] = "SPS2-GFP Tagged NAT";
                                            spNatJson["yoParent"] = spNatParent;
                                            spNatJson["yoMatingType"] = "alpha";
                                            spNatJson["yoMTA"] = mymta;
                                            spNatJson["yoPloidy"] = "Haploid";
                                            populateGeography(spNatJson, nvStrain);

                                            if (spNat1 != null && spNat1.length > 1) {
                                                //var spNat1Uri = "/addama-rest/dudley-sample/path/datasource/YO/" + spNat1 + "/annotations";
                                                if (spNat2 != null && spNat2.length > 1) {
                                                    spNatJson["yoSiblings"] = spNat2;
                                                }
                                                getProgenitor("yoKey:" + spNatParent, function() {
                                                    spNatJson["yoProgenitor"] = progenitor;
                                                    spNatJson["yoKey"] = spNat1;                                                    
                                                    postSolrUpdates(spNatJson, defaultCallback);

                                                    //postDudleyAnnotations(spNat1Uri, spNatJson, defaultCallback);
                                                });
                                            }
                                            if (spNat2 != null && spNat2.length > 1) {
                                                //var spNat2Uri = "/addama-rest/dudley-sample/path/datasource/YO/" + spNat2 + "/annotations";
                                                if (spNat1 != null && spNat1.length > 1) {
                                                    spNatJson["yoSiblings"] = spNat1;
                                                } else {
                                                    spNatJson["yoSiblings"] = "";
                                                }
                                                getProgenitor("yoKey:" + spNatParent, function() {
                                                    spNatJson["yoProgenitor"] = progenitor;
                                                    //postDudleyAnnotations(spNat2Uri, spNatJson, defaultCallback);                                                    
                                                    spNatJson["yoKey"] = spNat2;                                                    
                                                    postSolrUpdates(spNatJson, defaultCallback);
                                                });

                                            }
                                        }
                                }
                            }
                        ]
                    },

                    {
                        xtype: 'fieldset',
                        title: 'SPS2-GFP Tagged -> Haploid Parental Candidates',
                        width: 450,
                        collapsed: true,
                        collapsible: true,
                        items: [
                            {
                                layout: 'hbox',
                                width: 300,
                                fieldLabel: 'Haploid Parental Candidates',
                                msgTarget: 'under',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        value: 'KAN: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'kanCanStrain1Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    },
                                    {
                                        xtype: 'displayfield',
                                        value: 'NAT: '
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'natCanStrain1Id',
                                        width: 50,
                                        flex: 1,
                                        allowBlank: false,
                                        margins: '0 5 0 0',
                                        listeners: {change:
                                                function(field, nv, ov) {
                                                    field.setRawValue(transformYOKeyEntry(nv));
                                                }
                                        }
                                    }
                                ]
                            },
                            {
                                xtype: 'button',
                                fieldAlign: 'center',
                                text: '&nbsp;&nbsp;Save&nbsp;&nbsp;',
                                id: 'hpcButtonId',
                                listeners:{click:
                                        function() {
                                            var jsonParentPost = {};
                                            var kanStrain = Ext.getCmp("kanCanStrain1Id").getValue();
                                            if (kanStrain != null && kanStrain.length > 1) {
                                                jsonParentPost["yoStatus"] = "KAN Haploid Candidate";
                                                jsonParentPost["yoMatingType"] = "A";
                                                //var kanUri = "/addama-rest/dudley-sample/path/datasource/YO/" + kanStrain + "/annotations";
                                                getProgenitor("yoKey:" + kanStrain, function() {
                                                    jsonParentPost["yoProgenitor"] = progenitor;
                                                    jsonParentPost["yoKey"] = kanStrain;
                                                    postSolrUpdates(jsonParentPost, defaultCallback);
                                                });
                                                //postDudleyAnnotations(kanUri, jsonParentPost, defaultCallback);
                                            }
                                            var natStrain = Ext.getCmp("natCanStrain1Id").getValue();
                                            if (natStrain != null && natStrain.length > 1) {
                                                jsonParentPost["yoStatus"] = "NAT Haploid Candidate";
                                                jsonParentPost["yoMatingType"] = "alpha";
                                                //var natUri = "/addama-rest/dudley-sample/path/datasource/YO/" + natStrain + "/annotations";
                                                getProgenitor("yoKey:" + natStrain, function() {
                                                    jsonParentPost["yoProgenitor"] = progenitor;
                                                    jsonParentPost["yoKey"] = natStrain;
                                                    postSolrUpdates(jsonParentPost, defaultCallback);
                                                });
                                                //postDudleyAnnotations(natUri, jsonParentPost, defaultCallback);
                                            }
                                        }
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    });
    return yoICForm;
}

function popupQCForm(nvStrain) {
    mymta = nvStrain.mta;
    var qcConstructionWin = new Ext.Window({
        title: 'YO QC_Progeny Generation',
        width: 525,
        height: 600,
        autoscroll: true,
        modal: true,
        bodyStyle: 'padding:10px;',
        items: [setupInitialIsolate(nvStrain)],
        layout: 'fit'
    });
    qcConstructionWin.show();
}


function postProgenyAnnotations(progenyKey, siblingArray, progenyParent, progenyStatus, nvStrain) {
    //var progenyUri = "/addama-rest/dudley-sample/path/datasource/YO/" + progenyKey;
    var siblings = "";
    var jsonToPost = {};
    var matingTypeOffSet = 1;
    if (progenyStatus == "Homozygous Diploid") {
        jsonToPost["yoPloidy"] = "Diploid";
    }
    if (progenyStatus == "HO- Haploid") {
        matingTypeOffSet = 1; //changed from 2 to one
        jsonToPost["yoPloidy"] = "Haploid";
    }
    for (var i = 0; i < siblingArray.length; i++) {
        var strainVal = siblingArray[i];
        if (strainVal != "" && strainVal != progenyKey) {
            siblings = siblings + strainVal + "_";
        }
        if (strainVal == progenyKey) {
            if (i <= matingTypeOffSet) {
                jsonToPost["yoMatingType"] = "A";
            } else {
                jsonToPost["yoMatingType"] = "alpha";
            }
        }
    }
    if (siblings.length > 1) {
        siblings = siblings.substr(0, (siblings.length - 1));
    }
    jsonToPost["yoKey"] = progenyKey;
    jsonToPost["yoStatus"] = progenyStatus;
    jsonToPost["yoParent"] = progenyParent;
    jsonToPost["yoProgenitor"] = progenitor;
    jsonToPost["yoSiblings"] = siblings;
    jsonToPost["yoIsSC"] = "Yes";
    jsonToPost["yoMTA"] = mymta;
    populateGeography(jsonToPost, nvStrain);    
    postSolrUpdates(jsonToPost, defaultCallback);
}

/*
 If the strain's parent has a state of Initial Isolate, then
 that strain is the progenitor
 */
/*
function getProgenitor(strain, callback) {
    var uri = "/addama-rest/dudley-sample/path/datasource/YO/" + strain + "/annotations";
    Ext.Ajax.request({
        url: uri,
        method: 'GET',
        success: function(response) {
            var annoObj = Ext.util.JSON.decode(response.responseText);
            var status = annoObj.status;
            parentsJson[status] = annoObj.strainParents;
            //need this check otherwise program will go into an infinite loop as it traverses up 
            if (strain == annoObj.strainParents) {
                //Ext.Msg.alert("Warn", "Strain is labeled with itself as parent:" + strain);
                defaultMsg = "Please fix parent relationship: Strain annotated itself as parent:" + strain;
                //callback;
            }
            if (status != "Initial Isolate" && strain != annoObj.strainParents) {
                getProgenitor(annoObj.strainParents, callback);
            } else {
                //parentsJson["Initial Isolate"] = annoObj.strainParents;
                progenitor = strain;
                //Ext.Msg.alert("Info", "Got Initial Isolate strain:" + strain);
                callback();
            }
        },
        failure: function() {
            Ext.Msg.alert("Error", "Failed finding Initial Isolate parent for key:" + strain);
        }
    });
}
*/

function getProgenitor(strainTerm, callback) {
    var strain = strainTerm.split(":")[1]
    if (strain == "undefined" || !strain){
    	callback();
    	return;
    }
    var url = getSolrYoUrl(strainTerm,1);
    //"/solraddama-rest/dudley-sample/path/datasource/YO/" + strain + "/annotations";
    //getSolrYoUrl()
    Ext.Ajax.request({
        url: url,
        method: 'GET',
        success: function(response) {
            var resp = Ext.util.JSON.decode(response.responseText);
            var annoObj = resp.response.docs[0];
            var status = annoObj.yoStatus;
            //parentsJson[yoStatus] = status;
            //need this check otherwise program will go into an infinite loop as it traverses up 
            if (strain == annoObj.yoParent) {
                //Ext.Msg.alert("Warn", "Strain is labeled with itself as parent:" + strain);
                defaultMsg = "Please fix parent relationship: Strain annotated itself as parent:" + strain;
                //callback;
            }
            if (status != "Initial Isolate" && strain != annoObj.yoParent) {
                getProgenitor("yoKey:" + annoObj.yoParent, callback);
            } else {
                if (status == "Initial Isolate"){
                	progenitor = strain;
                }
                callback();
            }
        },
        failure: function() {
            Ext.Msg.alert("Error", "Failed finding Initial Isolate parent for key:" + strain);
        }
    });
}


function defaultCallback() {
    Ext.Msg.alert('Info', defaultMsg);
}

function transformYOKeyEntry(yoSearchInput) {
    if (yoSearchInput == null || yoSearchInput.length == 0) {
        return "";
    }
    yoSearchInput = yoSearchInput.toUpperCase();

    if (yoSearchInput.indexOf("YO") != -1) {
        return yoSearchInput.toUpperCase();
    } else if (yoSearchInput.indexOf("YO") != -1) {
        return "YO" + yoSearchInput.substr(2, yoSearchInput.length);
    } else {
        return "YO" + yoSearchInput;
    }

}